package cn.iocoder.yudao.module.system.controller.admin.chaoshun;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonSfEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.chaoshun.vo.sp.ChaoshunspRespVO;
import cn.iocoder.yudao.module.system.controller.admin.chaoshun.vo.sp.SpPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.chaoshun.ChaoshunSpDO;
import cn.iocoder.yudao.module.system.service.chaoshun.ChaoshunSpService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.system.enums.DictTypeConstants.*;

@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/chaoshun/courier")
@Validated
public class ChaoshunCourierController {

    @Resource
    private ChaoshunSpService chaoshunSpService;

    @GetMapping("/page")
    @Operation(summary = "获得商品列表")
    @PreAuthorize("@ss.hasPermission('chaoshun:courier:query')")
    public CommonResult<PageResult<ChaoshunspRespVO>> getCourierPage(@Valid SpPageReqVO pageReqVO) {
        // 获得商品信息分页列表
        PageResult<ChaoshunSpDO> pageResult = chaoshunSpService.getSpPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        // 拼接数据
        return success(new PageResult<>(CollectionUtils.convertList(pageResult.getList(), sp -> {
            ChaoshunspRespVO respVO = BeanUtil.copyProperties(sp, ChaoshunspRespVO.class);
            Integer yccs = respVO.getYccs();
            if (yccs > 0) {
                respVO.setTz(" 延" + yccs);
            }
            if (CommonSfEnum.isEnable(respVO.getSffh())) {
                respVO.setTz(StrUtil.trimToEmpty(respVO.getTz()) + " 返");
            }
            if (CommonSfEnum.isEnable(respVO.getSfbl())) {
                respVO.setTz(StrUtil.trimToEmpty(respVO.getTz()) + " 补");
            }
            respVO.setYffsmc(DictFrameworkUtils.getDictDataLabel(COMMON_YFZFFS, sp.getYffs()));
            respVO.setHkfsmc(DictFrameworkUtils.getDictDataLabel(COMMON_HKZFFS, sp.getHkfs()));
            respVO.setSfsmmc(DictFrameworkUtils.getDictDataLabel(COMMON_SF, sp.getSfsm()));
            return respVO;
        }),
                pageResult.getTotal()));
    }


}
